#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution
{
public:
    string removeOuterParentheses(string s)
    {
        string res;
        stack<char> ansStack;
        for (auto c : s)
        {
            if (c == ')')
            {
                ansStack.pop();
            }
            if (!ansStack.empty())
            {
                res.push_back(c);
            }
            if (c == '(')
            {
                ansStack.emplace(c);
            }
        }
        return res;
    }

    string removeOuterParentheses_2(string s)
    {
        int level = 0;
        string res;
        for (auto c : s)
        {
            if (c == ')')
            {
                level--;
            }
            if (level)
            {
                res.push_back(c);
            }
            if (c == '(')
            {
                level++;
            }
        }
        return res;
    }
};
int main()
{
    Solution s;
    string str = "(()())(())(()(()))";
    cout << s.removeOuterParentheses(str) << endl;
    system("pause");
    return 0;
}
